import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse, AxiosError } from 'axios'
import { message } from 'ant-design-vue'
import { stringify } from "qs"
// import store from '@/store'

// const base_url: string = process.env.NODE_ENV === 'development' ? '/sde' : (import.meta.env.VITE_APP_API_BASE_URL) as string

// create an axios instance
const service:AxiosInstance = axios.create({
    // baseURL: base_url,
    baseURL: (import.meta.env.VITE_APP_API_BASE_URL) as string,
    timeout: 5000,
    withCredentials: true,
})

// request interceptor
service.interceptors.request.use(
    (config: AxiosRequestConfig) => {
        if (config.method === 'post' && config.url !== '/meta/tInfoSjjZd/saveColumn') {
            config.data = stringify(config.data)
        }
        return config
    },
    (error: AxiosError) => {
        Promise.reject(error)
    }
)

// response interceptor
service.interceptors.response.use(
    (response: AxiosResponse) => {
        const res = response.data
        if (res.code !== 0) {
            message.warning(res.msg);
            return Promise.reject('error')
        } else {
            return response.data
        }
    },
    (error: AxiosError) => {
        message.warning(error.message);
        return Promise.reject(error)
    }
)

export default service